<?php
/**
 * @title 307. 区域和检索 - 数组可修改 - 硬累加
 * @author start2004
 */

// ini_set("memory_limit", "128M");

class NumArray {
    /**
     * @param Integer[] $nums
     */
    function __construct($nums) {
        $this->nums = $nums;
    }

    /**
     * @param Integer $i
     * @param Integer $val
     * @return NULL
     */
    function update($i, $val) {
        $this->nums[$i] = $val;
    }

    /**
     * @param Integer $i
     * @param Integer $j
     * @return Integer
     */
    function sumRange($i, $j) {
        /**
         * @since 2020-08-20 迭代递增
         */
        $sum = 0;
        for($k=$i; $k<=$j; $k++){
            $sum += $this->nums[$k];
        }

        /**
         * @return
         */
        return $sum;
    }
}



/**
 * @url http://127.0.0.1/leetcode/202008/2020.08.20_2.php
 */
$datas = [
    [
        ["NumArray","sumRange","update","sumRange"],
        [[[1,3,5]],[0,2],[1,2],[0,2]],
    ],
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    $obj = new $data[0][0](...$data[1][0]);

    for ($i=1; $i<count($data[0]); $i++){
        $result = $obj->$data[0][$i](...$data[1][$i]);

        echo $data[0][$i] ."(\"". $data[1][$i][0] ."\") ";
        if($result === true){
            echo "True";
        } elseif($result === false) {
            echo "False";
        } elseif($result === null) {
            echo "Null";
        } elseif(is_array($result)) {
            var_dump($result);
        } else {
            echo $result;
        }
        echo PHP_EOL;
    }

    echo str_repeat(PHP_EOL, 3);
}
$xhprof->end();
